package use_case.processRental;
import java.util.ArrayList;
import java.util.List;

import services.interfaces.ITaxCalculatorAdapter;
import services.interfaces.ServicesFactory;
import strategy.interfaces.IDiscountPricingStrategy;
import strategy.interfaces.PricingStrategyFactory;
import use_case.handlereturns.itemDescription;
public class Checkout  {
	
	private ArrayList<itemDescription> checkoutList;
	private List<TotalListener> listener;
	private ITaxCalculatorAdapter taxInterface;
	private double total; 
	private String taxType;
	
	public ArrayList<itemDescription> getCheckoutList( ) { return checkoutList; }
	
	//Constructor
	public Checkout()
	{
		checkoutList = new ArrayList<itemDescription>();
		listener = new ArrayList<TotalListener>();
		total = 0;
		taxType = "Composite";
	}
	
	//***********************************************
	//Observer Pattern 
	public void addTotalListener(TotalListener newListener) {
	    listener.add(newListener);
	  }
	
	public void publishTotalEvent(String name, double total){
		for (TotalListener tl : listener) {
		      tl.onTotalPropertyEvent(name, total);
		    }
	}
	//***********************************************
	
	//***********************************************
	// Strategy Pattern with Singleton Factory
	public void implementDiscount(String couponCode){
		PricingStrategyFactory factoryInstance = PricingStrategyFactory.getInstance();
		IDiscountPricingStrategy discountInterface = factoryInstance.getDiscountPricingStrategy(couponCode);
		setTotal(discountInterface.getDiscount(this));
		
	}
	
	//Adaptor Pattern with Singleton Factory
	public void addTaxes()
	{
		ServicesFactory factoryInstance = ServicesFactory.getInstance();
		taxInterface = factoryInstance.getTaxCalculatorAdapter(taxType);
		setTotal(taxInterface.getTaxes(total));
	}
	//***********************************************
	
	
	//Add item to Shopping Cart
	public void addItem(itemDescription i)
	{
		checkoutList.add(i);
		updateTotal();		
	}
		
	//Update Total Value
	private void updateTotal()
	{
		total = 0;
		for (int i = 0; i < checkoutList.size(); i++) {
	       	total += checkoutList.get(i).getCost();
	      }
		addTaxes();	
	}
		
	private void setTotal(double newTotal){
		total = newTotal;
		publishTotalEvent("checkout.total", getTotal());
	}
	
	
	// Return Shopping Cart List
	public String[] getSelectedList()
	{
		String[] selectionList= new String[checkoutList.size()];
		for(int i = 0; i<checkoutList.size(); i++)
		{
			selectionList[i] = checkoutList.get(i).getName() +"\t" +"$"+ checkoutList.get(i).getCost();
		}
		
		return selectionList;
	}

	// Remove Item from Shopping Cart
	public void removeItemFromCart(int itemToRemove) {
		checkoutList.remove(itemToRemove);
		updateTotal();
		publishTotalEvent("checkout.total", getTotal());
	}

	public int getCartSize() {return checkoutList.size();}
	
	public double getTotal(){return total;}
	
	public void changeTaxType(String newTax){taxType = newTax;}
	
	public itemDescription getItem(int index){return checkoutList.get(index);}
	
	public TotalListener getTotalListener(int index) {
	    return listener.get(index);
	  }


}
